<template>
  <div>
    <el-dialog
      :title="info.isadd ? '规格添加' : '规格编辑'"
      :visible.sync="info.isshow"
      @closed="closed"
    >
      <el-form :model="user"
        >{{ user }}
        {{ attrArr }}
        <el-form-item label="规格名称" label-width="120px">
          <el-input v-model="user.specsname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          label="规格属性"
          label-width="120px"
          v-for="(item, index) in attrArr"
          :key="index"
        >
          <div class="line">
            <el-input v-model="item.val" autocomplete="off"></el-input>
            <el-button
              type="primary"
              v-if="index == 0"
              @click="attrArr.push({ val: '' })"
              >新增规格属性</el-button
            >
            <el-button type="danger" v-else @click="attrArr.splice(index, 1)"
              >删除</el-button
            >
          </div>
        </el-form-item>
        <!-- <el-form-item label="规格属性" label-width="120px">
          <div class="line">
            <el-input v-model="user.catename" autocomplete="off"></el-input>
            <el-button type="danger">删除</el-button>
          </div>
        </el-form-item> -->

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add" v-if="info.isadd"
          >确 定添加</el-button
        >
        <el-button type="primary" @click="updata" v-else>确定修改</el-button>
      
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { IndexRoutes } from "../../../router";
import { reqspecsadd, reqspecsinfo, reqspecsedit } from "../../../http/api";
import { errorAlert, successAlert } from "../../../utils//alert";
import path from "path";
export default {
  props: ["info"],
  data() {
    return {
      // 菜单列表出数值
      menuList: [],

      //   表单数据
      user: {
        specsname: "",
        attrs: "",
        status: 2,
      },
      //  规格属性
      attrArr: [
        {
          val: "",
        },
        {
          val: "",
        },
      ],
    };
  },
  computed: {
    ...mapGetters({ list: "specs/list" }),
  },
  methods: {
    ...mapActions({ reqList: "specs/reqList" }),
    cancel() {
      this.info.isshow = false;
    },

    // 清空user
    empty() {
      this.user = {
        specsname: "",
        attrs: "",
        status: 2,
      };
      // 规格属性
      this.attrArr = [
        {
          val: "",
        },
        {
          val: "",
        },
    
      ];
      // this.$refs.input.files=null
    },
  
  // 检查数据
  checData(){
     return new Promise(resolve=>{
        if(this.user.specsname===""){
        errorAlert("规格名称不能为空")
      }
      if(this.attrArr.some(item=>item.val=='')){
        errorAlert("规格属性不能为空")
        return;
      }
      //attrArr[{"val":1},{"val:1"},{"val:2"}]=》["1","2","3"]
      console.log(this.attrArr);
     let newArr = this.attrArr.map((item) => item.val); //["1","2","1"]
       console.log(newArr);
        if (new Set(newArr).size < this.attrArr.length) {
          errorAlert("规格属性不能相同");
          return;
        }
      resolve()
     })
  },
    // 添加数据
    add() {
     
    this.checData().then(()=>{
           // 将attrArr赋值给attr [{val:1},{val:2}] 转成[1,2,3]
      this.user.attrs = this.attrArr.map((item) => item.val).join(",");

      reqspecsadd(this.user).then((res) => {
        if (res.data.code === 200) {
          successAlert(res.data.msg);
          // 清空·user
          console.log(res);
          this.empty();
          this.info.isshow = false;
          // 刷新列表
          this.reqList();
        }
      });
    })




 
    },
    // changpia() {
    //   if (this.user.pid ===0) {
    //     this.user.type = 1;
    //   } else {
    //     this.user.type = 2;
    //   }
    // },
    getone(id) {
      reqspecsinfo({ id: id }).then((res) => {
        console.log(res.data.code === 200);
        if (res.data.code == 200) {
          this.user = res.data.list[0];
          // 处理返回来的数据让字符串重新变成数组对象
          // this.user.attrs " [a,b]"变成  this.attrArr[{val:1},{val:2}]
          this.attrArr = this.user.attrs.map((item) => {
            return { val: item };
          });
          // this.imgUrl = this.$pre + this.user.img;
        }
      });
    },
    // 修改
    updata() {
      this.checData().then(()=>{
        this.user.attrs = this.attrArr.map((item) => item.val).join(",");
      reqspecsedit(this.user).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);

          this.empty();
          this.info.isshow = false;
          // 刷新列表
          this.reqList();
        }
      });
      })
    
    },
    closed() {
      // 若果是编辑的关闭 就执行empty
      // 不是添加
      if (!this.info.isadd) {
        this.empty();
      }
    },
  },
  mounted() {
    // 请求菜单列表数据
    // reqmenulist({ istree: true }).then((res) => {
    //   if (res.data.code == 200) {
    //     console.log(res);
    //     this.menuList = res.data.list;
    //   }
    // });
  },
};
</script>
<style lang="less" scoped>
@import "../../../less/index.less";
.line {
  display: flex;
  .el-input {
    flex: 1;
  }
}
</style>